package com.koc.robot;

import java.lang.Math;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

public class Util 
{
	public static int random(int begin, int end)
	{
		double randVal = Math.random();
		return (int)(begin + (end - begin) * randVal);
	}
	
	public static void rotatedsquare(DrawMode mode, float x, float y, float r, float d)
	{
		float a = d / 2 * (float)Math.sqrt(2);
		
		ByteBuffer vbb = ByteBuffer.allocateDirect(8 * Float.SIZE);
		vbb.order(ByteOrder.nativeOrder());
		FloatBuffer vertexBufferData = vbb.asFloatBuffer();
		
		for (int rot = 0; rot < 4; rot++)
		{
			float rotAngle = (float)Math.PI * rot * 0.5f;
			vertexBufferData.put(x + (float)Math.sin(rotAngle + r + (float)Math.PI / 4) * a);
			vertexBufferData.put(y + (float)Math.cos(rotAngle + r + (float)Math.PI / 4) * a);
		}
		
		// draw this polygon
		GraphicsHelper.polygon(mode, vertexBufferData);
	}
	
	public static void drawblock(float x, float y, float width, float height, Color fillcolor, Color outlinecolor, float r)
	{
		if (Float.compare((float)(r % (Math.PI / 2)), 0.0f) != 0)
		{
			GraphicsHelper.setColor(fillcolor);
			rotatedsquare(DrawMode.DRAW_FILL, x + width / 2 + 0.5f, y + height / 2 + 0.5f, r, width - 1);
		}
		else
		{
			GraphicsHelper.setColor(fillcolor);
			GraphicsHelper.rectangle(DrawMode.DRAW_FILL, x + 0.5f, y + 0.5f, width - 1, height - 1);
		}
	}
	
	public static int toColor(int r, int g, int b)
	{
		return android.graphics.Color.rgb(r, g, b);
	}
	
	public static int toColor(int r, int g, int b, int a)
	{
		return android.graphics.Color.argb(a, r, g, b);
	}
	
	static public boolean writeFloatToByteArray(float f, byte[] bytes, int offset, ByteOrder order)
	{
		if(offset + 4 > bytes.length)
			return false;
		
		int f_bits = Float.floatToIntBits(f);
		if(order == ByteOrder.BIG_ENDIAN)
		{
			bytes[offset + 3] = (byte)(f_bits & 0xFF);
			bytes[offset + 2] = (byte)((f_bits >> 8) & 0xFF);
			bytes[offset + 2] = (byte)((f_bits >> 16) & 0xFF);
			bytes[offset + 0] = (byte)((f_bits >> 24) & 0xFF);	
		}
		else //ByteOrder.LITTLE_ENDIAN
		{
			bytes[offset] = (byte)(f_bits & 0xFF);
			bytes[offset + 1] = (byte)((f_bits >> 8) & 0xFF);
			bytes[offset + 2] = (byte)((f_bits >> 16) & 0xFF);
			bytes[offset + 3] = (byte)((f_bits >> 24) & 0xFF);
		}
		return true;
	}
	
	static public boolean writeIntToByteArray(int i, byte[] bytes, int offset, ByteOrder order)
	{
		if(offset + 4 > bytes.length)
			return false;
		
		if(order == ByteOrder.BIG_ENDIAN)
		{
			bytes[offset + 3] = (byte)(i & 0xFF);
			bytes[offset + 2] = (byte)((i >> 8) & 0xFF);
			bytes[offset + 2] = (byte)((i >> 16) & 0xFF);
			bytes[offset + 0] = (byte)((i >> 24) & 0xFF);	
		}
		else //ByteOrder.LITTLE_ENDIAN
		{
			bytes[offset] = (byte)(i & 0xFF);
			bytes[offset + 1] = (byte)((i >> 8) & 0xFF);
			bytes[offset + 2] = (byte)((i >> 16) & 0xFF);
			bytes[offset + 3] = (byte)((i >> 24) & 0xFF);
		}
		return true;
	}
	
	static public boolean writeFloatToIntBuffer(float f, int[] buffer, int offset)
	{
		if(offset + 1 > buffer.length)
			return false;
		
		int fToInt = Float.floatToIntBits(f);
		buffer[offset] = fToInt;
		return true;
	}
}

enum DrawMode
{
	DRAW_LINE,
	DRAW_FILL,
}
